
package com.crowdfunding.circle.data.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crowdfunding.circle.common.controller.BaseController;
import com.crowdfunding.circle.data.entity.DataPlatformVO;
import com.crowdfunding.circle.data.param.DataPlatformParam;
import com.crowdfunding.circle.data.service.DataPlatformService;

/**
 * 数据_平台信息Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class DataPlatformController extends BaseController {
    
    private Logger logger = LoggerFactory.getLogger(DataPlatformController.class);
    
    @Autowired
    private DataPlatformService dataPlatformService;
    
    private final static String LIST_PAGE = "/data/platform_data_list_ajax";
    
    private final static String ADMIN_LIST_PAGE = "/admin/platform_data_manage";
    
    @RequestMapping(value = "/uncheck/dataPlatform/list")
    public ModelAndView queryList(DataPlatformParam dataPlatformParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        dataPlatformParam.setAuditStatus(2);
        List<DataPlatformVO> dataList = dataPlatformService.queryByPage(dataPlatformParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", dataPlatformParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/admin/dataPlatform/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String adminDelete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = dataPlatformService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除数据_平台信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除数据_平台信息失败");
            }
        } catch (Exception e) {
            logger.error("删除数据_平台信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除数据_平台信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/admin/dataPlatform/audit", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String audit(String platformId, Integer auditStatus) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            DataPlatformVO dataPlatformVO = new DataPlatformVO();
            
            dataPlatformVO.setPlatformId(platformId);
            dataPlatformVO.setAuditStatus(auditStatus);
            
            Integer affectCount = dataPlatformService.update(dataPlatformVO);
            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "审核数据_平台信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "审核数据_平台信息失败");
            }
        } catch (Exception e) {
            logger.error("审核数据_平台信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "审核数据_平台信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/admin/dataPlatform/list")
    public ModelAndView queryAdminList(DataPlatformParam dataPlatformParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        dataPlatformParam.setOrderDirection("DESC");

        dataPlatformParam.setOrderField("to_audit_count");

        List<DataPlatformVO> dataList = dataPlatformService.queryByPage(dataPlatformParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", dataPlatformParam);
        return new ModelAndView(ADMIN_LIST_PAGE, resultMap);
    }
}
